//
//  ATNetworksViewController.m
//  ATNetworks
//
//  Created by Ankit Thakur on 18/04/10.
//  Copyright _ 2010. All rights reserved.
//

#import "ATNetworksViewController.h"
#import "ATNetwork.h"
#import <Foundation/Foundation.h>
#import "ATJSONDispatcher.h"
#import "GridView.h"
#import "TableViewController.h"
@implementation ATNetworksViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	ATNetwork *network = [[ATNetwork alloc] init];
	[network openConnection:@"http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term=music&limit=1000" withCallBackTarget:self withCallBackSelector:@selector(callBackTargetSelector1:)];
	[network openConnection:@"http://m.delicious.com/" withCallBackTarget:self withCallBackSelector:@selector(callBackTargetSelector2:)];
	[network openConnection:@"http://www.oracle.com" withCallBackTarget:self withCallBackSelector:@selector(callBackTargetSelector3:)];
	[network openConnection:@"http://www.samsung.com" withCallBackTarget:self withCallBackSelector:@selector(callBackTargetSelector4:)];
	[network openConnection:@"http://www.macys.com" withCallBackTarget:self withCallBackSelector:@selector(callBackTargetSelector5:)];
	[network openConnection:@"http://www.miracle.com" withCallBackTarget:self withCallBackSelector:@selector(callBackTargetSelector0:)];
	[network release];
	network = nil;
    [super viewDidLoad];
}

- (void) callBackTargetSelector1:(NSData*)data{
	
//	NSLog(@"callBackTargetSelector1 -----> ");
	
	ATJSONDispatcher *jsonParser = [[ATJSONDispatcher alloc] init];
	NSDictionary *jsonString = [jsonParser decodeJSONData:data withEncoding:NSUTF8StringEncoding];
	NSLog(@"%@",jsonString);
	[jsonParser release];
	[self performSelector:@selector(performDataParsing:) withObject:(jsonString)];
}

- (void) performDataParsing:(NSDictionary*)data{
	
	NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[data objectForKey:@"results"]];
	
	for (int i = 0; i < [results count]; i++) {
		
		
		Music *music = [[Music alloc] init];
		NSMutableDictionary *localMusicData = [NSMutableDictionary new];
		music.name = [[results objectAtIndex:i] objectForKey:NAME];
		music.artist = [[results objectAtIndex:i] objectForKey:ARTIST];
		music.album = [[results objectAtIndex:i] objectForKey:ALBUM];
		music.imageUrl = [[results objectAtIndex:i] objectForKey:IMAGE_URL];
		music.price = [[results objectAtIndex:i] objectForKey:PRICE];
		music.time = [[results objectAtIndex:i] objectForKey:TIME];
		[results replaceObjectAtIndex:i withObject:music];
		[localMusicData release];
		localMusicData = nil;
		[music release];
		
	}
	
//	NSLog(@"%@",results);
	
	TableViewController *tbController = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];
	tbController.results = results;
	[results release];
	[self.navigationController pushViewController:tbController animated:YES];
	[tbController release];
	
	
}


- (void) callBackTargetSelector2:(NSData*)data{
//	NSLog(@"callBackTargetSelector2 -----> ");

}

- (void) callBackTargetSelector3:(NSData*)data{
//	NSLog(@"callBackTargetSelector3 -----> ");

}

- (void) callBackTargetSelector4:(NSData*)data{
//	NSLog(@"callBackTargetSelector4 -----> ");

}

- (void) callBackTargetSelector5:(NSData*)data{
//	NSLog(@"callBackTargetSelector5 ----->");

}

- (void) callBackTargetSelector0:(NSData*)data{
//	NSLog(@"callBackTargetSelector0 -----> ");

//	GridView *gridView = [[GridView alloc] initWithFrame:self.view.bounds];
//	gridView.backgroundColor = [UIColor clearColor];
//	[self.view addSubview:gridView];
//	[gridView release];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end


